﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace LanAnh
{
    public partial class frmDangNhap : Office2007Form 
    {
        public frmDangNhap()
        {
            InitializeComponent();
        }
        Data data = new Data();        
        string quyen;
        string ten;
        string pass;        
        string sql;        
        private void btnDangnhap_Click(object sender, EventArgs e)
        {
            if (txtTenDangNhap.Text.Replace(" ", "") == "")
            {
                labThongbao.Visible = true;
                labThongbao.Text = "Nhập Tên Đăng Nhập !";
                txtTenDangNhap.Focus();
            }
            else
            {
                labThongbao.Visible = false;
                sql = "SELECT * FROM tblDangNhap WHERE cTenDangNhap='" + txtTenDangNhap.Text + "'";                
                
                if (data.executeQueryTable(sql).Rows.Count == 0)
                {
                    labThongbao.Visible = true;
                    labThongbao.Text = "Tên đăng nhập không tồn tại !";
                    txtTenDangNhap.Focus();
                }
                else
                {
                    pass = data.executeQueryTable(sql).Rows[0]["cMatKhau"].ToString();
                    labThongbao.Visible = false;                    
                    if (getMd5Hash(txtMatKhau.Text) == pass)
                    {
                        ten = data.executeQueryTable(sql).Rows[0]["cTenDangNhap"].ToString();                        
                        quyen = data.executeQueryTable(sql).Rows[0]["cLoaiNguoiDung"].ToString();
                        string hoten = data.executeQueryTable(sql).Rows[0]["cHoTen"].ToString();
                        frmMain_2 main = new frmMain_2();
                        main.sktDangNhap(true);
                        main.sSessionTen(ten);
                        main.sSessionQuyen(quyen);
                        main.sHoTen(hoten);
                        
                        this.Close();
                    }
                    else
                    {
                        labThongbao.Visible = true;
                        labThongbao.Text = "Sai mật khẩu !";
                    }
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();

            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }            
            return   sBuilder.ToString();
        }  
        private void frmDangNhap_Load(object sender, EventArgs e)
        {
            
        }

        private void txtTenDangNhap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtMatKhau.Focus();

        }

        private void txtMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnDangnhap_Click(sender, e);
        }

    }
}
